package 链表;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
 */
public class _82_删除排序链表中的重复元素II {
    /**
     * 一次遍历
     * 定义一个当前节点，然后通过比较当前节点下一个的值与下下个的值，比较相邻两个节点的值是否相同，如果相同则循环删除指向
     * 将指向指到下一个目标节点，依次类推
     *
     * @param head 目标节点
     * @return ListNode
     */
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(0, head);
        ListNode curr = dummy;
        while (curr.next != null && curr.next.next != null) {
            if (curr.next.val == curr.next.next.val) {
                int target = curr.next.val;
                while (curr.next != null && curr.next.val == target) {
                    curr.next = curr.next.next;
                }
            }else {
                curr = curr.next;
            }
        }
        return dummy.next;
    }
}
